$.fn.sjld = function(shenfen, chengshi, quyu, categoryId) {
	var sfp = shenfen + ' p' // 省份
	var csp = chengshi + ' p' // 城市
	var qyp = quyu + ' p' // 区域
	var sfs = shenfen + ' .m_zlxg2'
	var css = chengshi + ' .m_zlxg2'
	var qys = quyu + ' .m_zlxg2'
	var sfli = shenfen + ' ul li'
	var csli = chengshi + ' ul li'
	var qyli = quyu + ' ul li'

	var L1 = 0;
	var L2 = 0,Li2 = 0;
	var L3 = 0,Li3 = 0;
	if (categoryId>0) {
		for ( var a = 0; a < category.length; a++) {
			var L1_flag = 0;
			var sfid = category[a].id;
			if (categoryId == sfid) {
				L1 = a;
				break;
			} else {
				var L2_flag = 0;
				var csgsmr = category[a].list;
				for ( var b = 0; b < csgsmr.length; b++) {
					var csid = csgsmr[b].id;
					if (csid == categoryId) {	
						L2 = b;
						Li2=1;
						L1_flag = 1;
						break;
					} else {
						var qygsmr = category[a].list[b].list;
						for ( var c = 0; c < qygsmr.length; c++) {
							var qyid = qygsmr[c].id;
							if (qyid == categoryId) {
								L3 = c;
								Li3=1;
								L2_flag=1;
								break;
							}
						}
					}
					if (L2_flag != 0) {
						L2 = b;
						Li2=1;
						L1_flag = 1;
						break;
					}
				}
			}
			if (L1_flag != 0) {
				L1 = a;
				break;
			}
		}
	}

	var sfgsmr = category;
	var csgsmr = category[L1].list;
	var qygsmr = category[L1].list[L2].list;

	var sfoid = sfgsmr[L1].id
	var csoid = csgsmr[L2].id
	var qyoid = qygsmr[L3].id

	$(sfp).text(sfgsmr[L1].name);
	$(sfp).attr('data', sfoid);
	$(csp).text(csgsmr[L2].name);
	$(csp).attr('data', csoid);
	$(qyp).text(qygsmr[L3].name);
	$(qyp).attr('data', qyoid);
	// 默认城市
	for ( var a = 0; a < sfgsmr.length; a++) {
		var sfmcmr = sfgsmr[a].name;
		var sfid = sfgsmr[a].id;
		var sfnrmr = "<li data='" + sfid + "'>" + sfmcmr + "</li>";
		if(L1==a &&categoryId>0){
			$("#sfdq_num").val(a);
			$(".category1").hide();
			$(".selectedCategory1").html("><a href='#0' onclick='minusSelect1()' data='"+sfid+"'>"+sfmcmr+"</a>");
		}
		$(shenfen).find('ul').append(sfnrmr);
	}
	
	for ( var b = 0; b < csgsmr.length; b++) {
		var csmcmr = csgsmr[b].name;
		var csid = csgsmr[b].id;
		var csnrmr = "<li data='" + csid + "'>" + csmcmr + "</li>";
		if(L2==b &&categoryId>0 &&Li2==1){
			$("#csdq_num").val(b);
			$(".category2").hide();
			$(".selectedCategory2").html("><a href='#0' onclick='minusSelect2()' data='"+csid+"'>"+csmcmr+"</a>");
		}
		$(chengshi).find('ul').append(csnrmr);

	}
	for ( var c = 0; c < qygsmr.length; c++) {
		var qymcmr = qygsmr[c].name;
		var qyid = qygsmr[c].id;
		var qynrmr = "<li data='" + qyid + "'>" + qymcmr + "</li>";
		if(L3==c &&categoryId>0 &&Li3==1){
			$(".category3").hide();
			$(".selectedCategory3").html("><a href='#0' onclick='minusSelect3()' data='"+qyid+"'>"+qymcmr+"</a>");
		}
		$(quyu).find('ul').append(qynrmr);
	}
	Array.max = function(array) {
		return Math.max.apply(Math, array);
	}
	
	/*---------------------------------------------------------------------*/

	$(sfli).live('click',function(){	
		$("#questionCategoryId").val($(this).attr("data"));
		var dqsf = $(this).text();
		var idsf = $(this).attr('data');
		$(shenfen).find('p').text(dqsf);
		$(shenfen).find('p').attr('title', dqsf);
		$(shenfen).find('p').attr('data', idsf);
		var sfnum = $(this).index();

		var csgs = category[sfnum].list;
		var csgs2 = category[sfnum].list[0].list;
		$(chengshi).find('ul').text('');

		for (i = 0; i < csgs.length; i++) {
			var csmc = csgs[i].name;
			var csid = csgs[i].id;
			var csnr = "<li data='" + csid + "'>" + csmc + "</li>";
			$(chengshi).find('ul').append(csnr);

		}
		Array.max = function(array) {
			return Math.max.apply(Math, array);
		}

		var qygsdqmr = category[sfnum].list[0].list;
		$(quyu).find('ul').text('');
		for (j = 0; j < qygsdqmr.length; j++) {
			var qymc = qygsdqmr[j].name;
			var qyid = qygsdqmr[j].id;
			var qynr = "<li data='" + qyid + "'>" + qymc + "</li>";
			$(quyu).find('ul').append(qynr);
		}
		$(csp).text(csgs[0].name);
		$(qyp).text(csgs2[0].name);
		$('#sfdq_num').val(sfnum);
		questionFormSubmit();
		
	})// 一级
	
	$(csli).live('click',function(){	
		$("#questionCategoryId").val($(this).attr("data"));
		var dqcs = $(this).text();
		var idcs = $(this).attr('data');
		var dqsf_num = $('#sfdq_num').val();
		if (dqsf_num == "") {
			dqsf_num = 0;
		} else {
			var dqsf_num = $('#sfdq_num').val();
		}
		$(chengshi).find('p').text(dqcs);
		$(chengshi).find('p').attr('title', dqcs);
		$(chengshi).find('p').attr('data', idcs);
		var csnum = $(this).index();
		var qygs = category[dqsf_num].list[csnum].list;
		$(quyu).find('ul').text('');
		for (j = 0; j < qygs.length; j++) {
			var qymc = qygs[j].name;
			var qyid = qygs[j].id
			var qynr = "<li data='" + qyid + "'>" + qymc + "</li>";
			$(quyu).find('ul').append(qynr);
		}
		$(qyp).text(qygs[0].name);
		$('#csdq_num').val(csnum);
		questionFormSubmit();
	}); // 二级
	

	$(qyli).live('click',function(){
		$("#questionCategoryId").val($(this).attr("data"));
		var dqqy = $(this).text();
		var idqy = $(this).attr('data');
		$(quyu).find('p').text(dqqy);
		$(quyu).find('p').attr('title', dqqy);
		$(quyu).find('p').attr('data', idqy);
		questionFormSubmit();
	});// 三级

	/*---------------------------------------------------------------------*/
	$('.m_zlxg').click(function() {
		$('#sfdq_tj').val($(sfp).text());
		$('#csdq_tj').val($(csp).text());
		$('#qydq_tj').val($(qyp).text());
	})// 表单传值获取

}
function minusSelect1(){
	$("#questionCategoryId").val("-1");
	questionFormSubmit();
}

function minusSelect2(){
	$("#questionCategoryId").val($(".selectedCategory1").find("a").attr("data"));
	questionFormSubmit();
}
function minusSelect3(){
	$("#questionCategoryId").val($(".selectedCategory2").find("a").attr("data"));
	questionFormSubmit();
}
